package com.study.zookeeper;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

public class MyWatcher implements Watcher {
    private ZooKeeper zooKeeper;
//    public MyWatcher(ZooKeeper zooKeeper) {
//        this.zooKeeper = zooKeeper;
//    }

    public void process(WatchedEvent event) {
        System.out.println(event);
        Event.EventType type = event.getType();
        if (type == Event.EventType.NodeCreated && event.getPath().startsWith("/zhaochao")) {
            System.out.println("赵超的数据新增 path = " + event.getPath());
        } else if (type == Event.EventType.NodeDataChanged && event.getPath().startsWith("/zhaochao")) {
            System.out.println("赵超的数据发生变化 path = " + event.getPath());
        }
        try {
            this.zooKeeper.exists(event.getPath(), true);
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
